package com.qiqi.web.man;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qiqi.dao.CatalogMapper;
import com.qiqi.dao.FeeMapper;
import com.qiqi.domain.Catalog;
import com.qiqi.domain.CatalogExample;
import com.qiqi.domain.Fee;
import com.qiqi.domain.FeeExample;

@Controller
public class FeeController {
	@Autowired
	private CatalogMapper catalogMapper;
	@Autowired
	private FeeMapper feeMapper;
	@RequestMapping("/man/fee")
	public String list(ModelMap model){
		CatalogExample example = new CatalogExample();
		example.createCriteria().andPidEqualTo(0);
		List<Catalog> list = catalogMapper.selectByExample(example);
		model.put("catalogs", list);
//		if(CollectionUtils.isNotEmpty(list)){
//			Catalog catalog = list.get(0);
//			FeeExample feequery = new FeeExample();
//			feequery.createCriteria().andCidEqualTo(catalog.getId());
//			List<Fee> fees = feeMapper.selectByExample(feequery);
//			model.put("fees", fees);
//		}
		return "/man/fee/fee";
	}
	@RequestMapping("/man/getFee")
	@ResponseBody
	public List<Fee> findFee(Integer id){
		FeeExample feequery = new FeeExample();
		feequery.createCriteria().andCidEqualTo(id);
		List<Fee> fees = feeMapper.selectByExample(feequery);
		return fees;
	}
	/**
	 * 更新套餐费用
	 * @param fee
	 * @return
	 */
	@RequestMapping("/man/fee/update")
	@ResponseBody
	public HashMap<String, Object> updateFee(Fee fee){
		HashMap<String, Object> res=new HashMap<String, Object>();
		int state=0;
		if(null==fee.getId()||0==fee.getId()){
			//添加
			state = feeMapper.insertSelective(fee);
		}else{
			//修改
			state=feeMapper.updateByPrimaryKeySelective(fee);
		}
		res.put("success", state>0);
		return res;
	}
	
	/**
	 * 更新套餐费用
	 * @param fee
	 * @return
	 */
	@RequestMapping("/man/fee/delete/{id}")
	@ResponseBody
	public HashMap<String, Object> deleteFee(@PathVariable Integer id){
		HashMap<String, Object> res=new HashMap<String, Object>();
		int state=0;
		state=feeMapper.deleteByPrimaryKey(id);
		res.put("success", state>0);
		return res;
	}
	
}
